import {
  personalMan,
  identifyCertification,
  getCity,
  getProvinces
} from "../../../../../api.js"

var app = getApp(); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province_two:[],
    province: [], //三联动
    city: [],
    area: [],
    index: -1,
    index2: -1,
    index3: -1,
    merProvince:"",
    merCity:"",
    merDistrict:"",
    city:"",
    area:"",
    onevalue:"",
    twovalue:"",
    treevalue:"",
    fourvalue:"如无实体经营场所，请填写身份证地址",
    code:[],
    HAND_IDCARD:{
      url:"http://document.growthupup.com/add_pic.png",
      thumb:"",
      base_image:""
    },
    IDCARD_BACK: {
      url: "http://document.growthupup.com/add_pic.png",
      thumb: "",
      base_image: ""
    },
    IDCARD_FRONT: {
      url: "http://document.growthupup.com/add_pic.png",
      thumb: "",
      base_image: ""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getCity({
      token: app.globalData.token
    }).then(res => {
      this.setData({
        province: res.data.data
      })
    })
    getProvinces({
      token: app.globalData.token
    }).then(res => {
      this.setData({
        province_two: res.data.data
      })
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  focusone(e){
    if (this.data.onevalue !="请输入你在平台中的商铺名称"){
      return
    }
    this.setData({
      onevalue:""
    })
  },
  oneblur(e){
    this.setData({
      onevalue:e.detail.value
    })
  },
  focustwo(e) {
    if (this.data.twovalue != "请确保与上传身份证上一致") {
      return
    }
    this.setData({
      twovalue: ""
    })
  },
  twoblur(e) {
    this.setData({
      twovalue: e.detail.value
    })
  },
  focustree(e) {
    if (this.data.treevalue != "请确保与上传身份证上一致") {
      return
    }
    this.setData({
      treevalue: ""
    })
  },
  treeblur(e) {
    this.setData({
      treevalue: e.detail.value
    })
  },
  focusfour(e) {
    if (this.data.fourvalue != "如无实体经营场所，请填写身份证地址") {
      return
    }
    this.setData({
      fourvalue: ""
    })
  },
  fourblur(e) {
    this.setData({
      fourvalue: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  //经营所在地
  bindProvince: function (e) {
    console.log(e)
    var that = this
    that.setData({
      index: e.detail.value,
      merProvince: that.data.province[e.detail.value].value,
      city: that.data.province[e.detail.value].children,
      index2: -1,
      index3: -1,
      area: []
    })
  },
  bindCity: function (e) {
    var that = this
    that.setData({
      index2: e.detail.value,
      merCity: that.data.city[e.detail.value].value,
      area: that.data.city[e.detail.value].children,
      index3: -1
    })
  },
  bindArea: function (e) {
    var that = this
    console.log(that.data.area[e.detail.value])
    that.setData({
      index3: e.detail.value,
      merDistrict: that.data.area[e.detail.value].value,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  // 第二步验证
  gonext(){
    if (this.data.onevalue != "" && this.data.onevalue != "请输入你在平台中的商铺名称" && this.data.twovalue != "" && this.data.twovalue != "请确保与上传身份证上一致" && this.data.treevalue != null && this.data.treevalue != "请确保与上传身份证上一致" && this.data.fourvalue != "" && this.data.fourvalue != "如无实体经营场所，请填写身份证地址" && this.data.HAND_IDCARD.thumb != "" && this.data.IDCARD_BACK.thumb !="" && this.data.IDCARD_FRONT.thumb != "" ){
      var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
      if (!idcardReg.test(this.data.treevalue)) {
          wx.showToast({
            title: '身份证格式不对',
            icon:"none"
          })
          return
      }
      identifyCertification({
        token:app.globalData.token,
        merFullName: this.data.onevalue,
        legalName: this.data.twovalue,
        legalIdCard: this.data.treevalue,
        merProvince: this.data.merProvince,
        merCity: this.data.merCity,
        merDistrict: this.data.merDistrict,
        merAddress: this.data.fourvalue,
        fileInfo:{
          IDCARD_FRONT:{
            url: this.data.IDCARD_FRONT.url,
            thumb: this.data.IDCARD_FRONT.thumb,
            _file: this.data.IDCARD_FRONT._file
          },
          IDCARD_BACK: {
            url: this.data.IDCARD_BACK.url,
            thumb: this.data.IDCARD_BACK.thumb,
            _file: this.data.IDCARD_BACK._file
          },
          HAND_IDCARD: {
            url: this.data.HAND_IDCARD.url,
            thumb: this.data.HAND_IDCARD.thumb,
            _file: this.data.HAND_IDCARD._file
          }
        }
      }).then(res=>{
        if(res.data.code==666){
         
          wx.navigateTo({
            url: '../second/second',
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:"none"
           
          })
        }
      })
      
    }else{
      wx.showToast({
        title: "请完整填写信息",
        icon: 'none'
      })
    }
   
  },

  //手持身份证
  HAND_IDCARD(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '正在上传图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.imgurl,
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function (res) {

            
            var jsonStr = res.data;

            jsonStr = jsonStr.replace(" ", "");
            
      
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
            }
            console.log(jj)
            if (jj.code == 666) {
              wx.hideLoading();
              that.setData({
              })
              that.data.HAND_IDCARD.url = jj.data.url
              that.data.HAND_IDCARD.thumb = jj.data.thumb
              that.data.HAND_IDCARD._file = jj.data.base_image
              that.setData({
                HAND_IDCARD: that.data.HAND_IDCARD
              })
            } else if (jj.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: jj.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },

  //上传身份证正面
  IDCARD_BACK(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '正在上传图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.imgurl,
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function (res) {


            var jsonStr = res.data;

            jsonStr = jsonStr.replace(" ", "");


            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
            }
            if (jj.code == 666) {
              wx.hideLoading();
              that.setData({
              })
              that.data.IDCARD_BACK.url = jj.data.url
              that.data.IDCARD_BACK.thumb = jj.data.thumb
              that.data.IDCARD_BACK._file = jj.data.base_image
              that.setData({
                IDCARD_BACK: that.data.IDCARD_BACK
              })
            } else if (jj.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: jj.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },

  //上传身份证国徽面
  IDCARD_FRONT(e) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        wx.showLoading({
          title: '正在上传图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.imgurl,
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function (res) {


            var jsonStr = res.data;

            jsonStr = jsonStr.replace(" ", "");


            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
            }
            console.log(jj)
            if (jj.code == 666) {
              wx.hideLoading();
              that.setData({
              })
              that.data.IDCARD_FRONT.url = jj.data.url
              that.data.IDCARD_FRONT.thumb = jj.data.thumb
              that.data.IDCARD_FRONT._file = jj.data.base_image
              that.setData({
                IDCARD_FRONT: that.data.IDCARD_FRONT
              })
            } else if (jj.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: jj.msg,
                icon: 'none'
              })
            }
          }
        })
      },
    })
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})